package main

import (
	"os"
	"fmt"
	"strconv"
	"time"
)

//二维空间一点
type point struct {
	i, j int
}

//上下左右
var dirs = [][]int{
	[]int{-1, 0}, []int{0, -1}, []int{1, 0}, []int{0, 1},
}

//两点相加
func (p point) add(r []int) point {
	return point{p.i + r[0], p.j + r[1]}
}

//点是否有效
func (p point) at(m [][]int) (int, bool) {
	if p.i < 0 || p.i >= len(m) || p.j < 0 || p.j >= len(m[0]) {
		return -1, false
	}
	return m[p.i][p.j], true
}

//读迷宫
func readMaze(filename string) [][]int {
	file, e := os.Open(filename)
	if (e != nil) {
		panic(e)
	}
	var row, col int
	fmt.Fscanf(file, "%d %d", &row, &col)
	maze := make([][]int, row)
	for i := range maze {
		maze[i] = make([]int, col)
		for j := range maze[i] {
			fmt.Fscanf(file, "%d", &maze[i][j])
		}
	}
	return maze
}

//打印迷宫
func printMaze(maze [][]int) {
	for i := range maze {
		for j := range maze[i] {
			fmt.Printf("%3d", maze[i][j])
		}
		fmt.Println()
	}
}

//走迷宫
func walkMaze(maze [][]int, start point, end point) ([][]int, []point) {
	//printMaze(maze)
	steps := make([][]int, len(maze))
	for i := range steps {
		for j := range maze[i] {
			steps[i] = make([]int, len(maze[i]))
			steps[i][j] = 0
		}
	}
	var q []point
	q = append(q, start)
	var q1 []point
	for len(q) > 0 {
		cur := q[0]
		q1 = append(q1, cur)
		q = q[1:]
		if cur == end {
			return steps, q1
		}
		for i := range dirs {
			next := cur.add(dirs[i])
			if at, b := next.at(maze); at == 1 || !b {
				continue
			}
			if at, b := next.at(steps); at != 0 || !b {
				continue
			}
			if next == start {
				continue
			}
			steps[next.i][next.j] = steps[cur.i][cur.j] + 1
			q = append(q, next)
		}
	}
	return steps, q1
}

func main() {
	//maze := readMaze("maze/maze.txt")
	//printMaze(maze)
	//fmt.Println("==================")
	//steps,q1 := walkMaze(maze, point{0, 0}, point{len(maze) - 1, len(maze[0]) - 1})
	//fmt.Println("==================")
	//fmt.Println(q1)
	//printMaze(steps)
	now := time.Now()
	for i := 0; i < 10000000; i++ {
		strconv.ParseFloat("3.1415926", 64)
		strconv.FormatFloat(3.1415926, 'E', -1, 64)
	}
	now2 := time.Now()
	fmt.Println((now2.Nanosecond() - now.Nanosecond())/100000)
}
